package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.groupshop.fragment.AllShangpinFragment;
import wnhz.com.business.groupshop.fragment.WeiShangjiaFragment;
import wnhz.com.business.groupshop.fragment.YiShangjiaFragment;
import wnhz.com.business.impl.ActionBarClickListener;

/**
 * 商品中心
 */
public class ShangpinActivity extends BaseActivity implements ActionBarClickListener {

    @BindView(R.id.iv_actionbar_left)
    ImageView ivActionbarLeft;
    @BindView(R.id.tv_actionbar_left)
    TextView tvActionbarLeft;
    @BindView(R.id.lay_actionbar_left)
    RelativeLayout layActionbarLeft;
    @BindView(R.id.tv_actionbar_title)
    TextView tvActionbarTitle;
    @BindView(R.id.iv_actionbar_seach)
    ImageView ivActionbarSeach;
    @BindView(R.id.iv_actionbar_release)
    ImageView ivActionbarRelease;
    @BindView(R.id.tv_tt)
    TextView tvTt;
    @BindView(R.id.tv_a1)
    TextView tvA1;
    @BindView(R.id.tv_allnums)
    TextView tvAllnums;
    @BindView(R.id.tv_a2)
    TextView tvA2;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.tv_b1)
    TextView tvB1;
    @BindView(R.id.tv_shangjianum)
    TextView tvShangjianum;
    @BindView(R.id.tv_b2)
    TextView tvB2;
    @BindView(R.id.ll_yishangjia)
    LinearLayout llYishangjia;
    @BindView(R.id.tv_c1)
    TextView tvC1;
    @BindView(R.id.tv_wishangjianums)
    TextView tvWishangjianums;
    @BindView(R.id.tv_c2)
    TextView tvC2;
    @BindView(R.id.ll_weishangjia)
    LinearLayout llWeishangjia;
    @BindView(R.id.layout_main_title)
    RelativeLayout layoutMainTitle;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.line2)
    View line2;
    @BindView(R.id.line3)
    View line3;
    @BindView(R.id.fragment_item)
    FrameLayout fragmentItem;


    private int type;
    private BaseFragment baseFragment;
    public static final int TYPE1 = 201;//全部
    public static final int TYPE2 = 202;//好评
    public static final int TYPE3 = 203;//中评
    private AllShangpinFragment allShangpinFragment;
    private YiShangjiaFragment yiShangjiaFragment;
    private WeiShangjiaFragment weiShangjiaFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shangpin);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            tvTt.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvTt.getLayoutParams();
            params.height = statusHeight;
            tvTt.setLayoutParams(params);
        }

        initview();

        if (savedInstanceState != null) {
            baseFragment = (BaseFragment) getSupportFragmentManager().getFragment(savedInstanceState, baseFragment.getClass().getSimpleName());
        } else {
            switch (type) {
                case TYPE1:
                    tvA1.setTextColor(getResources().getColor(R.color.red255));
                    tvAllnums.setTextColor(getResources().getColor(R.color.red255));
                    tvA2.setTextColor(getResources().getColor(R.color.red255));
                    line1.setVisibility(View.VISIBLE);
                    line2.setVisibility(View.INVISIBLE);
                    line3.setVisibility(View.INVISIBLE);
                    line1.setBackgroundColor(getResources().getColor(R.color.red255));
                    baseFragment = AllShangpinFragment.newInstance();

                    break;
                case TYPE2:
                    tvB1.setTextColor(getResources().getColor(R.color.red255));
                    tvShangjianum.setTextColor(getResources().getColor(R.color.red255));
                    tvB2.setTextColor(getResources().getColor(R.color.red255));
                    line1.setVisibility(View.INVISIBLE);
                    line2.setVisibility(View.VISIBLE);
                    line3.setVisibility(View.INVISIBLE);
                    line2.setBackgroundColor(getResources().getColor(R.color.red255));
                    baseFragment = YiShangjiaFragment.newInstance();

                    break;
                case TYPE3:
                    tvC1.setTextColor(getResources().getColor(R.color.red255));
                    tvWishangjianums.setTextColor(getResources().getColor(R.color.red255));
                    tvC2.setTextColor(getResources().getColor(R.color.red255));
                    line1.setVisibility(View.INVISIBLE);
                    line2.setVisibility(View.INVISIBLE);
                    line3.setVisibility(View.VISIBLE);
                    line2.setBackgroundColor(getResources().getColor(R.color.red255));
                    baseFragment = WeiShangjiaFragment.newInstance();

                    break;
            }
        }
        if (!baseFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_item, baseFragment, baseFragment.getClass().getSimpleName()).show(baseFragment).commit();
        }



    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, ShangpinActivity.class);
        return intent;
    }

    private void initview() {
        tvActionbarTitle.setText("商品中心");
        allShangpinFragment = new AllShangpinFragment();
        yiShangjiaFragment = new YiShangjiaFragment();
        weiShangjiaFragment = new WeiShangjiaFragment();
        type= TYPE1;

    }

    @Override
    public void onLeftClick() {
            finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }


    @OnClick({R.id.lay_actionbar_left, R.id.iv_actionbar_seach, R.id.iv_actionbar_release,R.id.ll_all, R.id.ll_yishangjia, R.id.ll_weishangjia})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lay_actionbar_left:
                finish();
                break;
            case R.id.iv_actionbar_seach:
                break;
            //商品发布
            case R.id.iv_actionbar_release:
                    startActivity(ShangPinSeadAvtivity.createIntent(this));
                break;
            case R.id.ll_all:
                tvA1.setTextColor(getResources().getColor(R.color.red255));
                tvAllnums.setTextColor(getResources().getColor(R.color.red255));
                tvA2.setTextColor(getResources().getColor(R.color.red255));
                tvB1.setTextColor(getResources().getColor(R.color.gray112));
                tvShangjianum.setTextColor(getResources().getColor(R.color.gray112));
                tvB2.setTextColor(getResources().getColor(R.color.gray112));
                tvC1.setTextColor(getResources().getColor(R.color.gray112));
                tvWishangjianums.setTextColor(getResources().getColor(R.color.gray112));
                tvC2.setTextColor(getResources().getColor(R.color.gray112));
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                turnToFragment(baseFragment.getClass(), AllShangpinFragment.class, null);
                break;
            case R.id.ll_yishangjia:
                tvA1.setTextColor(getResources().getColor(R.color.gray112));
                tvAllnums.setTextColor(getResources().getColor(R.color.gray112));
                tvA2.setTextColor(getResources().getColor(R.color.gray112));
                tvB1.setTextColor(getResources().getColor(R.color.red255));
                tvShangjianum.setTextColor(getResources().getColor(R.color.red255));
                tvB2.setTextColor(getResources().getColor(R.color.red255));
                tvC1.setTextColor(getResources().getColor(R.color.gray112));
                tvWishangjianums.setTextColor(getResources().getColor(R.color.gray112));
                tvC2.setTextColor(getResources().getColor(R.color.gray112));
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                turnToFragment(baseFragment.getClass(), YiShangjiaFragment.class, null);
                break;
            case R.id.ll_weishangjia:
                tvA1.setTextColor(getResources().getColor(R.color.gray112));
                tvAllnums.setTextColor(getResources().getColor(R.color.gray112));
                tvA2.setTextColor(getResources().getColor(R.color.gray112));
                tvB1.setTextColor(getResources().getColor(R.color.gray112));
                tvShangjianum.setTextColor(getResources().getColor(R.color.gray112));
                tvB2.setTextColor(getResources().getColor(R.color.gray112));
                tvC1.setTextColor(getResources().getColor(R.color.red255));
                tvWishangjianums.setTextColor(getResources().getColor(R.color.red255));
                tvC2.setTextColor(getResources().getColor(R.color.red255));
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                turnToFragment(baseFragment.getClass(), WeiShangjiaFragment.class, null);
                break;
        }
    }

    public void turnToFragment(Class<? extends BaseFragment> fromFragmentClass, Class<? extends BaseFragment> toFragmentClass, Bundle args) {

        FragmentManager fm = getSupportFragmentManager();
        //被切换的Fragment标签
        String fromTag = fromFragmentClass.getSimpleName();
        //切换到的Fragment标签
        String toTag = toFragmentClass.getSimpleName();
        //查找切换的Fragment
        Fragment fromFragment = fm.findFragmentByTag(fromTag);
        Fragment toFragment = fm.findFragmentByTag(toTag);
        //如果要切换到的Fragment不存在，则创建
        if (toFragment == null) {
            try {
                toFragment = toFragmentClass.newInstance();
                toFragment.setArguments(args);
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        //如果有参数传递，
        if (args != null && !args.isEmpty()) {
            toFragment.getArguments().putAll(args);
        }
        //Fragment事务
        FragmentTransaction ft = fm.beginTransaction();
        //设置Fragment切换效果
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                android.R.anim.fade_in, android.R.anim.fade_out);
        /**
         * 如果要切换到的Fragment没有被Fragment事务添加，则隐藏被切换的Fragment，添加要切换的Fragment
         * 否则，则隐藏被切换的Fragment，显示要切换的Fragment
         */
        if (!toFragment.isAdded()) {
            ft.hide(fromFragment).add(R.id.fragment_item, toFragment, toTag);
        } else {
            ft.hide(fromFragment).show(toFragment);
        }
        baseFragment = (BaseFragment) toFragment;
        //添加到返回堆栈
        //        ft.addToBackStack(tag);
        //不保留状态提交事务
        ft.commitAllowingStateLoss();
    }

}
